package invoicesystem.dao.impl;

import invoicesystem.dao.InvoicingRecordDao;
import invoicesystem.entry.Email;
import invoicesystem.entry.InvoicingRecord;
import invoicesystem.entry.Order;
import invoicesystem.pojo.*;
import invoicesystem.util.DBHelper;
import invoicesystem.util.PageHelper;

import java.util.ArrayList;

public class InvoicingRecordDaoImpl implements InvoicingRecordDao {

    private DBHelper db = new DBHelper();

    @Override
    public TitleAndAddress selectTitleAndAddress(Integer enterprise_id) {
        return null;
    }

    @Override
    public ArrayList<InvoicingApplyList> selectall() {
        return null;
    }

    @Override
    public ArrayList<InvoicingApplyList> selectByCreate_timeOrByTitleOramount(SelectTitleAndInvoic selTitle, PageHelper pageHelper) {
        StringBuilder sql = new StringBuilder("SELECT A.*,B.title " +
                "FROM invoicing_record A JOIN base_data B ON A.enterprise_id = B.enterprise_id " +
                "WHERE A.enterprise_id=? ");
        ArrayList list = new ArrayList();
        list.add(selTitle.getEnterprise_id());
        if (selTitle.getCreate_time() != null) {
            sql.append("and A.create_time = ? ");
            list.add(selTitle.getCreate_time());
        }
        if (selTitle.getTitle() != null) {
            sql.append("and B.title = ? ");
            list.add(selTitle.getTitle());
        }
        if (selTitle.getMinamount() != null) {
            sql.append("and ? <= A.amount ");
            list.add(selTitle.getMinamount());
        }
        if (selTitle.getMaxamount() != null) {
            sql.append("and A.amount<=? ");
            list.add(selTitle.getMaxamount());
        }
        if (selTitle.getStatus() != null) {
            if (selTitle.getStatus().equals("申请中")) {
                sql.append("and A.status='申请中' ");
            } else if (selTitle.getStatus().equals("已开票")) {
                sql.append("and A.status='已开票' ");
            } else if (selTitle.getStatus().equals("已作废")) {
                sql.append("and A.status='已作废' ");
            }

        }
        sql.append("limit ?,? ");
        int startindex = (pageHelper.getIndexPage() - 1) * pageHelper.getShowData();
        list.add(startindex);
        list.add(pageHelper.getShowData());
        return db.selectinfo(sql.toString(), InvoicingApplyList.class, list.toArray());
    }

    @Override
    public int updateTitleAndAddress(String title) {
        return 0;
    }

    @Override
    public int insertInvoicingRecord(InvoicingRecord invoicingRecord) {
        return 0;
    }



    @Override
    public int returnInvoicingRecord(Integer invoicingRecord_id) {
        System.out.println("----dao层id----" + invoicingRecord_id);

        String sql = "UPDATE invoicing_record A " +
                "JOIN `order` O ON A.id = O.invoicing_record_id " +
                "SET A.status = '已作废', O.invoicing_status = '未开票' " +
                "WHERE A.id = ?";
        System.out.println("----dao-----" + db.updateinfo(sql, invoicingRecord_id));
        return db.updateinfo(sql, invoicingRecord_id);
    }

    @Override
    public ArrayList selectMultitable(Integer id) {
        System.out.println("----dao---id" + id);

        String sql = "SELECT A.title,B.category,A.tax_no,B.type,B.email_id FROM base_data A JOIN invoicing_record B ON A.id = B.id WHERE A.id=?";
        return db.selectinfo(sql, InvoiceDetail.class, id);
    }

    @Override
    public ArrayList selectemail_id(Integer email_id) {
        String sql = "select * from email where id=?";
        return db.selectinfo(sql, Email.class, email_id);
    }

    @Override //查符合条件的数据数
    public int selectInvoicSize(SelectTitleAndInvoic selTitle) {
        StringBuilder sql = new StringBuilder("SELECT count(*) " +
                "FROM invoicing_record A JOIN base_data B ON A.enterprise_id = B.enterprise_id " +
                "WHERE A.enterprise_id=? ");
        ArrayList list = new ArrayList();
        list.add(selTitle.getEnterprise_id());

        if (selTitle.getCreate_time() != null) {
            sql.append("and A.create_time = ? ");
            list.add(selTitle.getCreate_time());
        }
        if (selTitle.getTitle() != null) {
            sql.append("and B.title = ? ");
            list.add(selTitle.getTitle());
        }
        if (selTitle.getMinamount() != null) {
            sql.append("and ? <= A.amount ");
            list.add(selTitle.getMinamount());
        }
        if (selTitle.getMaxamount() != null) {
            sql.append("and A.amount<=? ");
            list.add(selTitle.getMaxamount());
        }
        if (selTitle.getStatus() != null) {
            if (selTitle.getStatus().equals("申请中")) {
                sql.append("and A.status='申请中' ");
            } else if (selTitle.getStatus().equals("已开票")) {
                sql.append("and A.status='已开票' ");
            } else if (selTitle.getStatus().equals("已作废")) {
                sql.append("and A.status='已作废' ");
            }

        }
        return db.selectinfoCount(sql.toString(),list.toArray());
    }


    @Override
    public boolean updateInvoicingRecord(InvoicingRecord invoicingRecord) {
        String sql="insert into invoicing_record  values(null,?,888888,1,current_date,?,?,?,1,1,1,?)";
        int i= db.updateinfo(sql,invoicingRecord.getAmount(),invoicingRecord.getCategory(),invoicingRecord.getType(),invoicingRecord.getStatus(),invoicingRecord.getUplink_address());
        System.out.println(i);
        return i>0?true:false;
    }

}
